package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.DealerModel;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.DealerBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/8/14.
 */

public class DealerModelImpl implements DealerModel {
    private CallBack callBack;

    public DealerModelImpl(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void getDealerinfo(Context context, String dealerID) {
        HashMap<String, Object> build = new MapUtil()
                .put("dealer_id", dealerID).build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getDealerDetails()
                , new BaseSubscriber<TokenBean<DealerBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<DealerBean> o) {

                        callBack.callback(o.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }

                    @Override
                    public void onNext(TokenBean<DealerBean> o) {
                        super.onNext(o);
                    }

                    @Override
                    public void onStart() {
                        callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        callBack.loadCompleted();
                    }
                });
    }

    @Override
    public void editDealer(Context context, DealerBean dealerBean) {
//        address 否 str 车行地址
        HashMap<String, Object> build = new MapUtil()
                .put("id", dealerBean.getId())
                .put("dealer_name", dealerBean.getDealer_name())
                .put("boss", dealerBean.getBoss())
                .put("phone", dealerBean.getPhone())
                .put("market_id", dealerBean.getMarket_id())
                .put("floor", dealerBean.getFloor())
                .put("house_number", dealerBean.getHouse_number())
                .put("car_number", dealerBean.getCar_number())
                .put("month_sale", dealerBean.getMonth_sale())
                .put("financial_penetration", dealerBean.getFinancial_penetration())
                .put("financial_channel", dealerBean.getFinancial_channel())
                .put("kan_che", dealerBean.getKan_che())
                .put("xin_che", dealerBean.getXin_che())
                .put("car_preference", dealerBean.getCar_preference())
                .put("car_channel", dealerBean.getCar_channel())
                .put("merchant_analysis", dealerBean.getMerchant_analysis())
                .put("description", dealerBean.getDescription())
                .put("business_agent", dealerBean.getBusiness_agent())
                .put("dealer_photo", dealerBean.getDealer_photo())
                .put("account_number", dealerBean.getAccount_number())
                .put("bank_deposit", dealerBean.getBank_deposit())
                .put("bank_name", dealerBean.getBank_name()).build();


        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).editDealer()
                , new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }

                    @Override
                    public void onStart() {
                        callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        callBack.loadCompleted();
                    }
                });
    }

    @Override
    public void addDealer(Context context, DealerBean dealerBean) {
        HashMap<String, Object> build = new MapUtil()
                .put("dealer_name", dealerBean.getDealer_name())
                .put("boss", dealerBean.getBoss())
                .put("phone", dealerBean.getPhone())
                .put("market_id", dealerBean.getMarket_id())
                .put("floor", dealerBean.getFloor())
                .put("house_number", dealerBean.getHouse_number())
                .put("car_number", dealerBean.getCar_number())
                .put("month_sale", dealerBean.getMonth_sale())
                .put("financial_penetration", dealerBean.getFinancial_penetration())
                .put("financial_channel", dealerBean.getFinancial_channel())
                .put("kan_che", dealerBean.getKan_che())
                .put("xin_che", dealerBean.getXin_che())
                .put("car_preference", dealerBean.getCar_preference())
                .put("car_channel", dealerBean.getCar_channel())
                .put("merchant_analysis", dealerBean.getMerchant_analysis())
                .put("description", dealerBean.getDescription()).build();

        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).addDealer()
                , new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }

                    @Override
                    public void onStart() {
                        callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        callBack.loadCompleted();
                    }
                });
    }

    public interface CallBack {
        void callback(DealerBean bean);

        void loadStart();

        void loadCompleted();

        void loadFailure();
    }
}
